    import axios from 'axios'
    import store from '@/store'
    import {getToken, setToken} from '@/libs/util'
    import {Message, Modal} from 'iview'

    const addErrorLog = errorInfo => {
        const {statusText, status, 'request': {responseURL}} = errorInfo

        const info = {
            'type': 'ajax',
            'code': status,
            'mes': statusText,
            'url': responseURL
        }

        if (!responseURL.includes('save_error_logger')) {
            store.dispatch('addErrorLog', info)
        }
    }

    class HttpRequest {
        constructor(baseUrl = baseURL) {
            this.baseUrl = baseUrl
            this.queue = {}
        }

        getInsideConfig() {
            const config = {
                'baseURL': this.baseUrl,
                'headers': {
                    'Accept': 'application/vnd.app.v1+json'
                }
            }

            return config
        }

        destroy(url) {
            delete this.queue[url]
            if (!Object.keys(this.queue).length) {
                // Spin.hide()
            }
        }

        interceptors(instance, url) {
            // 请求拦截
            instance.interceptors.request.use(config => {
                /*
                 * 添加全局的loading...
                 * if (!Object.keys(this.queue).length) {
                 * Spin.show() // 不建议开启，因为界面不友好
                 * }
                 */
                this.queue[url] = true
                config.headers.Authorization = getToken()
                return config
            }, error => {
                return Promise.reject(error)
            })
            // 响应拦截
            instance.interceptors.response.use(res => {
                this.destroy(url)
                const {data, status} = res
                if (res.request.responseType === 'blob' && res.status === 200) {
                    return {data, status}
                }
                if (data.code != 0 || res.status != 200) {
                    if (data.code == '401') {
                        Modal.confirm({
                            'title': '登录已过期',
                            'onOk': () => {
                                setToken('');
                                window.location.href = "/login";
                            }
                        })
                    } else {
                        Message.error({content: data.message});
                    }

                    return Promise.reject({data, status})
                }
                return {data, status}
            }, error => {
                this.destroy(url)
                let errorInfo = error.response

                if (!errorInfo) {
                    const {'request': {statusText, status}, config} = JSON.parse(JSON.stringify(error))

                    errorInfo = {
                        statusText,
                        status,
                        'request': {'responseURL': config.url}
                    }
                }
                addErrorLog(errorInfo)
                if (errorInfo.data && errorInfo.data.message) {
                    Message.error({content: errorInfo.data.message});
                } else {
                    Message.error({content: errorInfo.statusText});
                }

                if (errorInfo.status == 401) {
                    setToken('');
                    window.location.href = "/login";
                }
                return Promise.reject(error)
            })
        }

        request(options) {
            const instance = axios.create()

            const newOptions = Object.assign(this.getInsideConfig(), options)

            if (newOptions.url != 'wechat/getwxacodeunlimit') {
                this.interceptors(instance, newOptions.url)
            }

            return instance(newOptions)
        }
    }

    export default HttpRequest
